package com.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName RabbitMQUtils
 * @Author HETAO
 * @Date 2020/5/4 18:58
 */
public class RabbitMQUtils {
    private static ConnectionFactory factory;

    static {
        factory = new ConnectionFactory();
        factory.setHost("yun.888808888.top");
        factory.setPort(5672);
        factory.setVirtualHost("/ems");
        factory.setUsername("hetao");
        factory.setPassword("jwdlh123");
    }

    //定义提供连接对象
    public static Connection getConnection() {
        try {
            return factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return null;
    }

    //关闭通道和关闭连接工具方法
    public static void closeConnectionAndChanel(Channel channel, Connection connection) {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
